<template>
  <div class="home">
    <div class="countdown-app">
      <h1>🎉 倒计时</h1>
      <div class="input-group">
        <el-text size="small" class="target-time">目标时间：</el-text>
        <el-date-picker
          v-model="targetDate"
          type="datetime"
          placeholder="请选择目标日期时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="x"
          class="responsive-input"
        />
      </div>
      <p class="subtitle">距离目标时间还有：</p>
      <div class="countdown-timer">
        <div class="time-unit">
          <span class="number">{{ timeLeft.days }}</span>
          <span class="label">天</span>
        </div>
        <div class="time-unit">
          <span class="number">{{ timeLeft.hours }}</span>
          <span class="label">时</span>
        </div>
        <div class="time-unit">
          <span class="number">{{ timeLeft.minutes }}</span>
          <span class="label">分</span>
        </div>
        <div class="time-unit">
          <span class="number">{{ timeLeft.seconds }}</span>
          <span class="label">秒</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "HomeView",
  data() {
    return {
      targetDate: new Date("2025-12-31 23:59:59").getTime(),
      timeLeft: {
        days: 0,
        hours: 0,
        minutes: 0,
        seconds: 0,
      },
      timer: null,
    };
  },
  methods: {
    calculateTimeLeft() {
      const now = new Date().getTime();
      const difference = this.targetDate - now;

      if (difference > 0) {
        this.timeLeft = {
          days: Math.floor(difference / (1000 * 60 * 60 * 24)),
          hours: Math.floor(
            (difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
          ),
          minutes: Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)),
          seconds: Math.floor((difference % (1000 * 60)) / 1000),
        };
      } else {
        this.timeLeft = { days: 0, hours: 0, minutes: 0, seconds: 0 };
        clearInterval(this.timer);
      }
    },
    startTimer() {
      this.clearTimer();
      this.timer = setInterval(() => {
        this.calculateTimeLeft();
      }, 1000);
    },
    clearTimer() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },
  mounted() {
    this.startTimer();
  },
  beforeUnmount() {
    this.clearTimer();
  },
  watch: {
    targetDate(newVal) {
      if (newVal && typeof newVal === "number") {
        this.calculateTimeLeft();
        this.startTimer();
      }
    },
  },
};
</script>

<style scoped>
/* 全局重置（防止 body 默认 margin 导致滚动） */
html,
body {
  margin: 0;
  padding: 0;
}

.home {
  height: calc(100vh - 16px);
  overflow: hidden; /* 🔒 彻底禁止滚动 */
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa; /* 柔和背景，避免纯白刺眼 */
}

.countdown-app {
  text-align: center;
  padding: 2rem;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}

h1 {
  font-size: 2.2rem;
  margin-bottom: 1.2rem;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
  margin: 1.2rem 0;
}

.input-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  align-items: center;
  margin-bottom: 1.5rem;
}

.target-time {
  color: white;
  font-weight: 600;
}

:deep(.el-date-editor) {
  width: 220px;
}

.countdown-timer {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin: 2.5rem 0;
  flex-wrap: wrap;
}

.time-unit {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(6px);
  padding: 1.2rem 0.8rem;
  border-radius: 16px;
  min-width: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.2s ease;
}

.time-unit:hover {
  transform: translateY(-4px);
  background: rgba(255, 255, 255, 0.25);
}

.number {
  font-size: 2.8rem;
  font-weight: 800;
  line-height: 1;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.label {
  font-size: 1rem;
  opacity: 0.85;
  margin-top: 0.4rem;
  letter-spacing: 1px;
}

/* 响应式优化 */
@media (max-width: 768px) {
  .home {
    padding: 1rem;
  }

  .countdown-app {
    padding: 1.5rem;
    border-radius: 16px;
  }

  h1 {
    font-size: 1.8rem;
  }

  .number {
    font-size: 2.2rem;
  }

  .time-unit {
    min-width: 70px;
    padding: 1rem 0.6rem;
  }

  :deep(.el-date-editor) {
    width: 180px;
    font-size: 0.9rem;
  }
}

@media (max-width: 480px) {
  .countdown-timer {
    gap: 1rem;
  }

  .number {
    font-size: 1.8rem;
  }

  .label {
    font-size: 0.9rem;
  }
}
</style>
